package com.yb.xczx.cloud.content.controller;


import com.yb.core.model.PageResult;
import com.yb.core.model.XczxResult;
import com.yb.core.web.db.model.XczxPage;
import com.yb.xczx.cloud.content.entity.CourseBase;
import com.yb.xczx.cloud.content.service.ICourseBaseService;
import com.yb.xczx.cloud.content.vo.AddCourseVo;
import com.yb.xczx.cloud.content.vo.CourseBaseInfoVo;
import com.yb.xczx.cloud.content.vo.CourseBaseVo;
import com.yb.xczx.cloud.content.vo.UpdateCourseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author 姚兵
 * @since 2023-05-07
 */
@RestController
@RequestMapping("/course")
@Api(value = "课程管理接口",tags = {"课程管理接口"})
public class CourseBaseController {

    @Autowired
    ICourseBaseService courseBaseService;


    @GetMapping("")
    @ApiOperation("查询所有")
    @PreAuthorize("hasAuthority('ADMIN')")
    public XczxResult<List<CourseBase>> list(@RequestParam(required = false) @ApiParam("请求msg") String msg, @RequestParam(required = false)  @ApiParam("请求code") Integer code){
        return XczxResult.success(courseBaseService.list());
    }


    @GetMapping("/one")
    @ApiOperation("查询单个")
    public XczxResult<CourseBase> one(@ApiParam("id") @RequestParam Integer id){
        return XczxResult.success(courseBaseService.getById(id));
    }

    @PostMapping("/page")
    @ApiOperation("分页查询")
    public XczxResult<XczxPage> page(@ApiParam("分页查询参数") @RequestBody XczxPage<CourseBaseVo> page){

        return courseBaseService.page(page);
    }

    @PostMapping("list")
    public PageResult<CourseBase> list(Long pageNo,Long pageSize,@RequestBody CourseBaseVo condition){

        XczxPage<CourseBaseVo> page = new XczxPage<>();
        page.setCondition(condition);
        condition.setName(condition.getCourseName());
        condition.setStatus(condition.getPublishStatus());
        page.setSize(pageSize);
        page.setCurrent(pageNo);

        XczxResult<XczxPage<CourseBase>> result = courseBaseService.page(page);

        XczxPage<CourseBase> data = result.getData();

        PageResult<CourseBase> pageResult = new PageResult<>(data.getRecords(), data.getTotal(), data.getCurrent(), data.getSize());

        return pageResult;
    }

    @PostMapping("")
    @ApiOperation("新增课程")
    public CourseBaseInfoVo createCourseBase(@Valid @RequestBody AddCourseVo courseVo){
        Long companyId=113114115l;
        return courseBaseService.createCourseBase(companyId,courseVo);
    }


    @GetMapping("/{courseId}")
    public CourseBaseInfoVo getCourseBaseInfo(@PathVariable String courseId){
        return courseBaseService.getCourseBaseInfo(courseId);
    }

    @PutMapping("")
    @ApiOperation("新增课程")
    public CourseBaseInfoVo updateCourseBase(@Valid @RequestBody UpdateCourseVo courseVo){
        Long companyId=113114115l;
        return courseBaseService.updateCourseBase(companyId,courseVo);
    }
}
